package hd.cls9;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class ChatServer {
    //聊天
    public static void main(String[] args) {
        try {
            ServerSocket st = new ServerSocket(9999);
            System.out.println("启动");
            while (true) {
                Socket s = st.accept();
                System.out.println(s + "连接了");
                InputStream in = s.getInputStream();
                OutputStream out = s.getOutputStream();
                //>>>转换字符流，方便聊天
                PrintWriter pw = new PrintWriter(out, true);//
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                while (true) {
                    String ask = br.readLine();
                    pw.println("你说的话：" + ask + ",朕知道了");
                    if (ask.equals("byebye"))
                        break;
                    //>>>pw.flush();
                }
                s.close();//关闭
                System.out.println(s + "关闭了");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
